home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 June: Reference Library / Dev.CD Jun 00 RL Disk 1.toast / pc / technical documentation / develop / develop issue 27 / develop issue 27 code / internet config assistant / internetassistant / tinternetassistantinterview.cp < prev    next >
Encoding:
Text File  |  1996-06-30  |  13.5 KB  |  519 lines

  1. /*
  2.     File:        TInternetAssistantDialog.cp
  3.  
  4.     Contains:    Dialog implementing the Internet Setup Assistant
  5.  
  6.     Written by:    Arno Gourdol
  7.  
  8.     Copyright:    © 1996 by Apple Computer, Inc., all rights reserved.
  9.  
  10. */
  11.  
  12. #include "TInternetAssistantInterview.h"
  13. #include "assert.h"
  14. #include "TApplication.h"
  15.  
  16. #include <Resources.h>
  17. #include "ICTypes.h"
  18. #include "ICKeys.h"
  19. #include "ICAPI.h"
  20.  
  21. void GetHostName(ConstStr255Param address, Str255 host);
  22.  
  23.  
  24.  
  25. // --------------------------------------------------------------------
  26. // GetHostName
  27. // --------------------------------------------------------------------
  28. // Extract the host from a "user@host" string
  29.  
  30. void GetHostName(ConstStr255Param address, Str255 host)
  31. {
  32.     UInt16 atIndex = 0;        // Index of the "@" character in the string
  33.     
  34.     host[0] = 0;
  35.     
  36.     for (UInt16 i = 1; i <= address[0]; i++)
  37.     {
  38.         if (address[i] == '@')
  39.         {
  40.             atIndex = i;
  41.             break;
  42.         }
  43.     }
  44.     
  45.     if (atIndex > 0 && atIndex <= address[0])
  46.     {
  47.         host[0] = address[0] - atIndex;
  48.         BlockMoveData(&address[atIndex + 1], &host[1], host[0]);
  49.     }
  50. }
  51.  
  52.  
  53.  
  54.  
  55. // --------------------------------------------------------------------
  56. // TInternetAssistantDialog
  57. // --------------------------------------------------------------------
  58.  
  59. TInternetAssistantDialog::TInternetAssistantDialog(TApplication* application) :
  60.     TAssistant(kIntroPane, kNNTPHostPane, application),
  61.     fInternetConfig('ICEx'),
  62.     fMap(302, 303, 300, 301, 300)
  63. {
  64.     // Get the default values from ICS and other sources
  65.     // and store them in the parameters (<email>, <organization>...)
  66.     GetDefaultValues();
  67. }
  68.  
  69.  
  70.  
  71. // --------------------------------------------------------------------
  72. // ~TInternetAssistantDialog
  73. // --------------------------------------------------------------------
  74.  
  75. TInternetAssistantDialog::~TInternetAssistantDialog()
  76. {
  77. }
  78.  
  79.  
  80.  
  81. // --------------------------------------------------------------------
  82. // DrawUserItem
  83. // --------------------------------------------------------------------
  84. // Hook function called to draw custom user items
  85. // We use it to draw the custom map
  86.  
  87. void TInternetAssistantDialog::DrawUserItem(TDrawContext& drawContext, 
  88.                                             short item, 
  89.                                             const CRect& frame)
  90. {
  91.     TAssistant::DrawUserItem(drawContext, item, frame);
  92.     
  93.     if (GetDialogPane() == kLocationPane && item == kMapUserItem)
  94.     {
  95.         fMap.Draw(drawContext, frame);    
  96.     }
  97. }
  98.  
  99.  
  100.  
  101. // --------------------------------------------------------------------
  102. // FilterEvent
  103. // --------------------------------------------------------------------
  104. // Hook function called to allow us to filter events
  105. // We use it to handle the map tracking on mouseDown
  106.  
  107. Boolean TInternetAssistantDialog::FilterEvent(const EventRecord& event)
  108. {
  109.     Boolean result = false;
  110.     
  111.     if (event.what == mouseDown && GetDialogPane() == kLocationPane)
  112.     {
  113.         // If a mousedown happened in the location pane...
  114.         CPoint where(event.where);
  115.         TDrawContext drawContext(GetGrafPtr());
  116.         if (drawContext.Lock())
  117.         {
  118.             drawContext.ConvertFromScreen(where);
  119.             // ... and it was the map user item...
  120.             if (GetItemRect(kMapUserItem).Contains(where))
  121.             {
  122.                 // ... pass the mouseDown to the map
  123.                 // We have to do this because only mouse-ups (thru ItemHit)
  124.                 // are passed usually to user items.
  125.                 fMap.MouseDown(GetItemRect(kMapUserItem));
  126.                 result = true;
  127.             }
  128.             drawContext.Unlock();
  129.         }
  130.     }
  131.     
  132.     if (!result)
  133.     {
  134.         result = TAssistant::FilterEvent(event);
  135.     }
  136.     
  137.     return result;
  138. }
  139.  
  140.  
  141.  
  142. // --------------------------------------------------------------------
  143. // FilterKey
  144. // --------------------------------------------------------------------
  145. // Hook function called when a key is pressed.
  146. // Return true if the key was handled.
  147. // We use it to allow return in the signature field
  148.  
  149. Boolean TInternetAssistantDialog::FilterKey(const EventRecord& event, UInt16 key)
  150. {
  151.     Boolean result;
  152.     
  153.     if (key == keyReturn && GetDialogPane() == kSignaturePane)
  154.     {
  155.         short start;
  156.         short end;
  157.         
  158.         GetSelectionEndpoints(start, end);
  159.         
  160.         if (start == 0 && end >= GetItemTextSize())
  161.         {
  162.             // If the text is entirely selected,
  163.             // handle return, etc. as shortcuts
  164.             result = TAssistant::FilterKey(event, key);
  165.         }
  166.         else
  167.         {
  168.             result = false;
  169.         }
  170.     }
  171.     else
  172.     {
  173.         result = TAssistant::FilterKey(event, key);
  174.     }
  175.     
  176.     return result;
  177. }
  178.  
  179.  
  180.  
  181. // --------------------------------------------------------------------
  182. // Pulse
  183. // --------------------------------------------------------------------
  184. // Hook function called at Idle time.
  185. // We use it to build bitmaps for the map
  186.  
  187. void TInternetAssistantDialog::Pulse(void)
  188. {
  189.     // If the location pane is the current pane...
  190.     if (GetDialogPane() == kLocationPane)
  191.     {
  192.         // give some time to the map item.
  193.         // The Map item will use it to build the bitmaps
  194.         // for the different regions
  195.         // (since this operation takes some time, we do 
  196.         //  it asynchronously)
  197.         fMap.Pulse();
  198.     }
  199.     
  200.     TAssistant::Pulse();
  201. }
  202.  
  203.  
  204.  
  205. // --------------------------------------------------------------------
  206. // ItemHit
  207. // --------------------------------------------------------------------
  208. // Hook function called when an item is selected
  209.  
  210. void TInternetAssistantDialog::ItemHit(UInt16 pane, short item)
  211. {
  212.  
  213.     if (pane == kLocationPane)
  214.     {
  215.         // If the location pane was clicked, get the location
  216.         // that was clicked on from the fMap object.
  217.         Str255 value;
  218.         GetIndString(value, 300, (fMap.GetValue() - 1) * 4 + 1);
  219.         SetParameter("\p<location>", value);
  220.     }
  221.     else if (pane == kEmailOnePane)
  222.     {
  223.         if (item == kPasswordField)
  224.         {
  225.             // Clicked (or typed) in the password field
  226.             // Check to see if the password has changed.
  227.             // If so, save the value in fPassword
  228.             {
  229.                 Str255 newPassword;
  230.                 Str255 oldPassword;
  231.                 
  232.                 GetIndString(oldPassword, 301, 4);
  233.                 oldPassword[0] = fPassword[0];
  234.                 GetItemText(kPasswordField, newPassword);
  235.  
  236.                 if (fPasswordChanged || 
  237.                     !EqualString(newPassword, oldPassword, true, true))
  238.                 {
  239.                     fPasswordChanged = true;
  240.                     BlockMoveData(newPassword, 
  241.                                         fPassword, newPassword[0] + 1);
  242.                 }
  243.             }
  244.             // If the password was not changed, it was probably
  245.             // clicked-on. Select it.
  246.             if (!fPasswordChanged)
  247.                 SelectItemText(kPasswordField);
  248.         }
  249.         else
  250.         {
  251.             // Clicked on some other item...
  252.             // replace the password by bullets
  253.             // The real password is contained in fPassword
  254.             Str255 bulletsString;
  255.             
  256.             GetIndString(bulletsString, 301, 4);    // "•••••••••"
  257.             bulletsString[0] = fPassword[0];
  258.             SetParameter("\p<password>", bulletsString);
  259.             SetItemText(kPasswordField, bulletsString);
  260.             {
  261.                 SInt32 ignore;
  262.                 Delay(8, &ignore);
  263.             }
  264.             fPasswordChanged = false;
  265.         }
  266.     }
  267.  
  268.     // Some other item. Let the superclass handle it
  269.     TAssistant::ItemHit(pane, item);
  270. }
  271.  
  272.  
  273.  
  274. // --------------------------------------------------------------------
  275. // SetDialogPane
  276. // --------------------------------------------------------------------
  277. // Change to the indicated dialog pane
  278. // Overriden here so we get a chance to set default values for 
  279. // panes before they get displayed
  280.  
  281. void TInternetAssistantDialog::SetDialogPane(SInt16 pane)
  282. {
  283.     if (pane == kSignaturePane)
  284.     {
  285.         Str255 signature;
  286.         Str255 emailAddress;
  287.         GetParameter("\p<signature>", signature);
  288.         GetParameter("\p<email>", emailAddress);
  289.         // If no signature was specificed, but an e-mail address was...
  290.         if (signature[0] == 0 && emailAddress[0] != 0)
  291.         {
  292.             GetIndString(signature, 301, 1);
  293.             SetParameter("\p<signature>", signature);
  294.         }
  295.     }
  296.     else if (pane == kEmailOnePane)
  297.     {
  298.         // When the password pane is displayed, note that
  299.         // the password hasn't been changed yet.
  300.         fPasswordChanged = false;
  301.     }
  302.     else if (pane == kEmailTwoPane)
  303.     {
  304.         Str255 emailAccount;
  305.         Str255 emailHost;
  306.         GetParameter("\p<email account>", emailAccount);
  307.         GetParameter("\p<SMTP host>", emailHost);
  308.         // If the e-mail account or host are not specified...
  309.         if (emailAccount[0] == 0 || emailHost[0] == 0)
  310.         {
  311.             // ... assign them default values based on the host
  312.             // extracted from the e-mail address
  313.             Str255 emailAddress;
  314.             Str255 host;
  315.             GetParameter("\p<email>", emailAddress);
  316.             GetHostName(emailAddress, host);
  317.             if (emailAccount[0] == 0)
  318.                 SetParameter("\p<email account>", host);
  319.             if (emailHost[0] == 0)
  320.                 SetParameter("\p<SMTP host>", host);
  321.         }
  322.     }
  323.     else if (pane == kHomePagePane)
  324.     {
  325.         Str255 homePage;
  326.         
  327.         GetParameter("\p<home page>", homePage);
  328.         // If there is no home page suggested already...
  329.         if (homePage[0] == 0)
  330.         {
  331.             // ... propose one
  332.             GetIndString(homePage, 301, 3);
  333.             SetParameter("\p<home page>", homePage);
  334.         }
  335.     }
  336.     
  337.     TAssistant::SetDialogPane(pane);
  338. }
  339.  
  340.  
  341.  
  342. // --------------------------------------------------------------------
  343. // GetDefaultValues
  344. // --------------------------------------------------------------------
  345. // Collect default values from Internet Config
  346. // ??? We could also collect the default location from the Map 
  347. //     control panel
  348.  
  349. void TInternetAssistantDialog::GetDefaultValues(void)
  350. {
  351.     OSErr err;
  352.     
  353.     err = fInternetConfig.ICBegin(icReadOnlyPerm);
  354.     if (err == noErr)
  355.     {
  356.         Str255 value;
  357.         
  358.         fInternetConfig.ICGetString(kICRealName, value);
  359.         if (value[0] == 0)
  360.         {
  361.             // If no name set, get the one from FileSharing
  362.             StringHandle userName;
  363.             userName = GetString(kUserNameSTR);
  364.             if (userName != NULL)
  365.             {
  366.                 BlockMoveData(*userName, value, *userName[0] + 1);
  367.                 ReleaseResource((Handle)userName);
  368.             }
  369.         }
  370.         SetParameter("\p<user name>", value);
  371.         fInternetConfig.ICGetString(kICOrganization, value);
  372.         SetParameter("\p<organization>", value);
  373.         fInternetConfig.ICGetText(kICSignature, value);
  374.         SetParameter("\p<signature>", value);
  375.         fInternetConfig.ICGetString(kICEmail, value);
  376.         SetParameter("\p<email>", value);
  377.         fInternetConfig.ICGetString(kICMailAccount, value);
  378.         SetParameter("\p<email account>", value);
  379.         fInternetConfig.ICGetScrambledString(kICMailPassword, fPassword);
  380.         GetIndString(value, 301, 4);
  381.         value[0] = fPassword[0];
  382.         SetParameter("\p<password>", value);
  383.         fInternetConfig.ICGetString(kICSMTPHost, value);
  384.         SetParameter("\p<SMTP host>", value);
  385.         fInternetConfig.ICGetString(kICNNTPHost, value);
  386.         SetParameter("\p<NNTP host>", value);
  387.         fInternetConfig.ICGetString(kICWWWHomePage, value);
  388.         SetParameter("\p<home page>", value);
  389.                 
  390.         err = fInternetConfig.ICEnd();
  391.         assert(err == noErr);
  392.     }
  393.     else
  394.     {
  395.         Str255 value;
  396.         value[0] = 0;
  397.         
  398.         SetParameter("\p<user name>", value);
  399.         SetParameter("\p<organization>", value);
  400.         SetParameter("\p<signature>", value);
  401.         SetParameter("\p<email>", value);
  402.         SetParameter("\p<email account>", value);
  403.         SetParameter("\p<password>", value);
  404.         SetParameter("\p<SMTP host>", value);
  405.         SetParameter("\p<home page>", value);
  406.         SetParameter("\p<NNTP host>", value);
  407.     }
  408. }
  409.  
  410.  
  411.  
  412. // --------------------------------------------------------------------
  413. // GoAhead
  414. // --------------------------------------------------------------------
  415. // Save the information collected during the interview 
  416. // to Internet Config
  417.  
  418. void TInternetAssistantDialog::GoAhead(void)
  419. {
  420.     OSErr err = fInternetConfig.ICBegin(icReadWritePerm);
  421.     if (err == noErr)
  422.     {
  423.         Str255 value;
  424.         
  425.         GetParameter("\p<user name>", value);
  426.         fInternetConfig.ICSetString(kICRealName, value);
  427.         
  428.         GetParameter("\p<organization>", value);        
  429.         fInternetConfig.ICSetString(kICOrganization, value);
  430.  
  431.         GetParameter("\p<signature>", value);
  432.         fInternetConfig.ICSetText(kICSignature, value);
  433.         
  434.         GetParameter("\p<email>", value);
  435.         fInternetConfig.ICSetString(kICEmail, value);
  436.  
  437.         GetParameter("\p<email account>", value);
  438.         fInternetConfig.ICSetString(kICMailAccount, value);
  439.  
  440.         fInternetConfig.ICSetScrambledString(kICMailPassword, fPassword);
  441.  
  442.         GetParameter("\p<SMTP host>", value);
  443.         fInternetConfig.ICSetString(kICSMTPHost, value);
  444.  
  445.         GetParameter("\p<NNTP host>", value);
  446.         fInternetConfig.ICSetString(kICNNTPHost, value);
  447.  
  448.         GetParameter("\p<home page>", value);
  449.         fInternetConfig.ICSetString(kICWWWHomePage, value);
  450.     
  451.         {
  452.             SInt16 mapValue = fMap.GetValue();
  453.             if (mapValue > 0)
  454.             {
  455.                 GetIndString(value, 300, (mapValue - 1) * 4 + 2);
  456.                 fInternetConfig.ICSetString(kICArchiePreferred, value);
  457.                 GetIndString(value, 300, (mapValue - 1) * 4 + 3);
  458.                 fInternetConfig.ICSetString(kICInfoMacPreferred, value);
  459.                 GetIndString(value, 300, (mapValue - 1) * 4 + 4);
  460.                 fInternetConfig.ICSetString(kICUMichPreferred, value);
  461.             }
  462.         }
  463.     
  464.         err = fInternetConfig.ICEnd();
  465.         assert(err == noErr);
  466.     }
  467. }
  468.  
  469.  
  470.  
  471. // --------------------------------------------------------------------
  472. // SkipPane
  473. // --------------------------------------------------------------------
  474. // Return true if we want to skip a pane
  475. // We show the "e-mail error" pane only if the e-mail entered
  476. // did not contain a host name
  477.  
  478. Boolean TInternetAssistantDialog::SkipPane(SInt16 pane)
  479. {
  480.     Boolean result = false;
  481.  
  482.     if (pane == kEmailError)
  483.     {
  484.         // Check to see if we want to skip the mail error pane
  485.         // This pane is displayed only if the e-mail address entered
  486.         // does not contain a host specification (i.e. a @ character)
  487.         Str255 emailAddress;
  488.         Str255 host;
  489.         GetParameter("\p<email>", emailAddress);
  490.         GetHostName(emailAddress, host);
  491.         result = host[0] != 0;
  492.     }
  493.     
  494.     return result;
  495. }
  496.  
  497.  
  498.  
  499. // --------------------------------------------------------------------
  500. // NextPane
  501. // --------------------------------------------------------------------
  502. // Function called to query the ID of the next pane.
  503. // Returns 0 if no more panes
  504. // Override this function if you need to present panes in non-sequential
  505. // order or if you want to skip a long sub-interview.
  506. // For the Internet Assistant, we could have made all the panes 
  507. // sequentials. We have the "e-mail error" pane out of sequence 
  508. // to demonstate this technique.
  509.  
  510. SInt16 TInternetAssistantDialog::NextPane(SInt16 currentPane)
  511. {
  512.     if (currentPane == kEmailOnePane)
  513.         return kEmailError;
  514.     else if (currentPane == kEmailError)
  515.         return kEmailTwoPane;
  516.     else
  517.         return TAssistant::NextPane(currentPane);
  518. }
  519.